var express = require('express');
var router = express.Router();
let md5 = require('md5');
let config = require('../config/config');
let {User, Session} = require('../database/sequelize');
let {isEmail, isPhone} = require('../utils/utils');
let {successResult, paramsError, isPhoneAndEMailRepeat} = require('../base/result');

/**
 * 获取用户列表，分页
 */
router.get('/bms/user/list', async function (req, res, next) {

  let page = req.body.page;
  let order = req.body.order;
  let sortBy = req.body.sortBy;
  let limit = req.body.limit;

  //过滤
  if (!page || page < 1 || isNaN(page)) {
    page = 1
  }

  if (order !== 'desc' && order !== 'asc') {
    order = 'desc';
  }

  if (sortBy) {
    sortBy = "id"
  }

  if (!limit || limit < 1 || isNaN(limit)) {
    limit = 10;
  }
  let data = [];
//order: [[sortBy, order]]

  await User.findAll({offset: (page - 1) * limit, limit: limit})
    .then(user => {
      for (let item of user) {
        delete item.dataValues.password;
        data.push(item.dataValues);
      }
    });


  if (data) {
    res.send(successResult(data));
  } else {
    res.send({"success": false, "msg": "失败"});
  }


});


/**
 * 获取用户详情信息
 */
router.get('/bms/user/detail', async function (req, res, next) {

  let id = req.body.id;

  //过滤
  if (!id || isNaN(id)) {
    res.send({"success": false, "msg": "参数错误"});
    return
  }
  let data = [];

  await User.findById(id)
    .then(user => {
      delete user.dataValues.password;
      data = user.dataValues;

    });


  if (data) {
    res.send({"success": true, "msg": "success", data: data});
  } else {
    res.send({"success": false, "msg": "失败"});
  }


});


/**
 * 添加用户信息
 */
router.post('/bms/user/add', async function (req, res, next) {

  let name = req.body.name;
  let phone = req.body.phone;
  let email = req.body.email;
  let password = req.body.password;
  let avatar = req.body.avatar;
  let sex = req.body.sex;

  //过滤
  if (!name || !phone || !email || !password || !isPhone(phone) || !isEmail(email)) {
    res.send({"success": false, "msg": "参数错误"});
    return
  }

  if (!sex || (sex != 1 && sex != 2)) {
    sex = 1;
  }

  let data = [];

  password = md5(config.salt + password);

  let isRepeat = await isPhoneAndEMailRepeat(phone, email);
  console.log(isRepeat);

  if (isRepeat) {
    res.send({"success": false, "msg": "账号或者邮箱重复"});
    return
  }

  await User.create({
    id: null,
    name: name,
    phone: phone,
    email: email,
    password: password,
    avatar: avatar,
    sex: sex,
    role_level: 1,
    updated_at: new Date().getTime() / 1000,
    created_at: new Date().getTime() / 1000
  }).then(user => {
    data = user.dataValues;
  });


  if (data) {
    res.send({"success": true, "msg": "success", data: data});
  } else {
    res.send({"success": false, "msg": "失败"});
  }

});


/**
 * 更新个人用户信息
 */
router.post('/bms/user/update', async function (req, res, next) {

  let id = req.body.id;
  let name = req.body.name;
  let phone = req.body.phone;
  let email = req.body.email;
  let password = req.body.password;
  let avatar = req.body.avatar;
  let sex = req.body.sex;

  //过滤
  if (!id || isNaN(id) || !name || !phone || !email || !password || !isPhone(phone) || !isEmail(email)) {
    res.send({"success": false, "msg": "参数错误"});
    return
  }

  if (!sex || (sex != 1 && sex != 2)) {
    sex = 1;
  }

  let data = [];

  password = md5(config.salt + password);

  let isRepeat = await isPhoneAndEMailRepeat(phone, email);

  if (isRepeat) {
    res.send({"success": false, "msg": "账号或者邮箱重复"});
    return
  }

  await User.update({
    name: name,
    phone: phone,
    email: email,
    password: password,
    avatar: avatar,
    sex: sex,
    updated_at: new Date().getTime() / 1000,
  }, {
    where: {id: id}
  }).then(user => {
    data = user[0];
  });

  if (data) {
    res.send({"success": true, "msg": "success", data: data});
  } else {
    res.send({"success": false, "msg": "失败"});
  }

});


/**
 * 删除用户记录
 */
router.post('/bms/user/delete', async function (req, res, next) {

  let id = req.body.id;
  //过滤
  if (!id || isNaN(id)) {
    res.send({"success": false, "msg": "参数错误"});
    return
  }


  let data = {};

  await User.destroy({
    where: {id: id}
  }).then(user => {
    data = user;
  });

  if (data) {
    res.send({"success": true, "msg": "success", data: data});
  } else {
    res.send({"success": false, "msg": "失败"});
  }

});


module.exports = router;
